#include "Task.hpp"
#include "BlockQueue.hpp"
#include <iostream>
#include <ctime>
#include <unistd.h>
#include <string>
#include <pthread.h>

using namespace std;

const string ops = "+-*/%";

void* consumer(void* args)
{
    BlockQueue<Task>* bqp = static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        Task t = bqp->pop();
        int one, two;
        char op;
        t.get(&one, &two, &op);
        cout << "consumer["<< pthread_self() << "]" << one << op << two <<"=" << t.run() << endl;
        sleep(1);
    }
}

void* producer(void* args)
{
    BlockQueue<Task>* bpq = static_cast<BlockQueue<Task>*>(args);
    while (true)
    {
        int one = rand()%50;
        int two = rand()%50;
        char op = ops[rand()%ops.size()];
        Task t(one, two, op);
        bpq->push(t);
        cout << "producer[" << pthread_self() << "]" << one << op << two <<"=" << "?\n"; 
    }
}

int main()
{
    srand(time(nullptr));

    BlockQueue<Task> bq;
    pthread_t con, pro1, pro2, pro3;
    pthread_create(&con, nullptr, consumer, &bq);
    pthread_create(&pro1, nullptr, producer, &bq);
    pthread_create(&pro2, nullptr, producer, &bq);
    pthread_create(&pro3, nullptr, producer, &bq);

    pthread_join(con, nullptr);
    return 0;
}
